﻿using Api.Common;
using Api.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.DAL
{
    /// <summary>
    /// 数据访问类:report
    /// </summary>
    public partial class report
    {
        private string databaseprefix = "eazy_"; //数据库表名前缀
        public report()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "report");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists_I(int wf_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "report");
            strSql.Append(" where Instance_id=@Instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@Instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = wf_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
		/// 增加一条数据
		/// </summary>
        public int Add(Models.report model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 添加主表数据====================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into " + databaseprefix + "report(");
                        strSql.Append("Instance_id,user_name,sex,birthday,h_address,h_mobile,company,represent,c_address,c_mobile,case_title,case_time,content,bm_yijian,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fg_yijian,fg_remark,fg_qianm,fg_time,fgld_yijian,fgld_remark,fgld_qianm,fgld_time,add_time)");
                        strSql.Append(" values (");
                        strSql.Append("@Instance_id,@user_name,@sex,@birthday,@h_address,@h_mobile,@company,@represent,@c_address,@c_mobile,@case_title,@case_time,@content,@bm_yijian,@bm_remark,@bm_qianm,@bm_time,@zf_remark,@zf_qianm,@zf_time,@fg_yijian,@fg_remark,@fg_qianm,@fg_time,@fgld_yijian,@fgld_remark,@fgld_qianm,@fgld_time,@add_time)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
                                    new SqlParameter("@Instance_id", SqlDbType.Int,4),
                                    new SqlParameter("@user_name", SqlDbType.NVarChar,100),
                                    new SqlParameter("@sex", SqlDbType.Int,4),
                                    new SqlParameter("@birthday", SqlDbType.NVarChar,20),
                                    new SqlParameter("@h_address", SqlDbType.NVarChar,255),
                                    new SqlParameter("@h_mobile", SqlDbType.NVarChar,20),
                                    new SqlParameter("@company", SqlDbType.NVarChar,100),
                                    new SqlParameter("@represent", SqlDbType.NVarChar,20),
                                    new SqlParameter("@c_address", SqlDbType.NVarChar,255),
                                    new SqlParameter("@c_mobile", SqlDbType.NVarChar,20),
                                    new SqlParameter("@case_title", SqlDbType.NVarChar,255),
                                    new SqlParameter("@case_time", SqlDbType.DateTime),
                                    new SqlParameter("@content", SqlDbType.NText),
                                    new SqlParameter("@bm_yijian", SqlDbType.Int,4),
                                    new SqlParameter("@bm_remark", SqlDbType.NText),
                                    new SqlParameter("@bm_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@bm_time", SqlDbType.DateTime),
                                    new SqlParameter("@zf_remark", SqlDbType.NText),
                                    new SqlParameter("@zf_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@zf_time", SqlDbType.DateTime),
                                    new SqlParameter("@fg_yijian", SqlDbType.Int,4),
                                    new SqlParameter("@fg_remark", SqlDbType.NText),
                                    new SqlParameter("@fg_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@fg_time", SqlDbType.DateTime),
                                    new SqlParameter("@fgld_yijian", SqlDbType.Int,4),
                                    new SqlParameter("@fgld_remark", SqlDbType.NText),
                                    new SqlParameter("@fgld_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@fgld_time", SqlDbType.DateTime),
                                    new SqlParameter("@add_time", SqlDbType.DateTime)};
                        parameters[0].Value = model.Instance_id;
                        parameters[1].Value = model.user_name;
                        parameters[2].Value = model.sex;
                        parameters[3].Value = model.birthday;
                        parameters[4].Value = model.h_address;
                        parameters[5].Value = model.h_mobile;
                        parameters[6].Value = model.company;
                        parameters[7].Value = model.represent;
                        parameters[8].Value = model.c_address;
                        parameters[9].Value = model.c_mobile;
                        parameters[10].Value = model.case_title;
                        parameters[11].Value = model.case_time;
                        parameters[12].Value = model.content;
                        parameters[13].Value = model.bm_yijian;
                        parameters[14].Value = model.bm_remark;
                        parameters[15].Value = model.bm_qianm;
                        parameters[16].Value = model.bm_time;
                        parameters[17].Value = model.zf_remark;
                        parameters[18].Value = model.zf_qianm;
                        parameters[19].Value = model.zf_time;
                        parameters[20].Value = model.fg_yijian;
                        parameters[21].Value = model.fg_remark;
                        parameters[22].Value = model.fg_qianm;
                        parameters[23].Value = model.fg_time;
                        parameters[24].Value = model.fgld_yijian;
                        parameters[25].Value = model.fgld_remark;
                        parameters[26].Value = model.fgld_qianm;
                        parameters[27].Value = model.fgld_time;
                        parameters[28].Value = model.add_time;
                        //添加主表数据
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.id = Convert.ToInt32(obj);
                        #endregion

                        //#region 修改内容附件==========================
                        ////删除已删除的附件
                        //new case_attach(databaseprefix).DeleteList_I(conn, trans, model.attach, model.Instance_id);
                        //// 添加/修改附件
                        //if (model.attach != null)
                        //{
                        //    StringBuilder strSql4;
                        //    foreach (Model.case_attach modelt in model.attach)
                        //    {
                        //        strSql4 = new StringBuilder();
                        //        if (modelt.id > 0)
                        //        {
                        //            strSql4.Append("update " + databaseprefix + "case_attach set ");
                        //            strSql4.Append("instance_id=@instance_id,");
                        //            strSql4.Append("step_id=@step_id,");
                        //            strSql4.Append("file_name=@file_name,");
                        //            strSql4.Append("file_path=@file_path,");
                        //            strSql4.Append("file_size=@file_size,");
                        //            strSql4.Append("file_ext=@file_ext,");
                        //            strSql4.Append("point=@point,");
                        //            strSql4.Append("user_name=@user_name,");
                        //            strSql4.Append("article_id=@article_id");
                        //            strSql4.Append(" where id=@id");
                        //            SqlParameter[] parameters4 = {
                        //                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                        //                    new SqlParameter("@step_id", SqlDbType.Int,4),
                        //                    new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                        //                    new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                        //                    new SqlParameter("@file_size", SqlDbType.Int,4),
                        //                    new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                        //                    new SqlParameter("@point", SqlDbType.Int,4),
                        //                    new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                        //                    new SqlParameter("@article_id", SqlDbType.Int,4),
                        //                    new SqlParameter("@id", SqlDbType.Int,4)};
                        //            parameters4[0].Value = modelt.instance_id;
                        //            parameters4[1].Value = modelt.step_id;
                        //            parameters4[2].Value = modelt.file_name;
                        //            parameters4[3].Value = modelt.file_path;
                        //            parameters4[4].Value = modelt.file_size;
                        //            parameters4[5].Value = modelt.file_ext;
                        //            parameters4[6].Value = modelt.point;
                        //            parameters4[7].Value = modelt.user_name;
                        //            parameters4[8].Value = modelt.article_id;
                        //            parameters4[9].Value = modelt.id;
                        //            DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                        //        }
                        //        else
                        //        {
                        //            strSql4.Append("insert into " + databaseprefix + "case_attach(");
                        //            strSql4.Append("instance_id,step_id,file_name,file_path,file_size,file_ext,down_num,point,user_name,article_id)");
                        //            strSql4.Append(" values (");
                        //            strSql4.Append("@instance_id,@step_id,@file_name,@file_path,@file_size,@file_ext,@down_num,@point,@user_name,@article_id)");
                        //            SqlParameter[] parameters4 = {
                        //                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                        //                    new SqlParameter("@step_id", SqlDbType.Int,4),
                        //                    new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                        //                    new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                        //                    new SqlParameter("@file_size", SqlDbType.Int,4),
                        //                    new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                        //                    new SqlParameter("@down_num", SqlDbType.Int,4),
                        //                    new SqlParameter("@point", SqlDbType.Int,4),
                        //                    new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                        //                    new SqlParameter("@article_id", SqlDbType.Int,4)};
                        //            parameters4[0].Value = modelt.instance_id;
                        //            parameters4[1].Value = modelt.step_id;
                        //            parameters4[2].Value = modelt.file_name;
                        //            parameters4[3].Value = modelt.file_path;
                        //            parameters4[4].Value = modelt.file_size;
                        //            parameters4[5].Value = modelt.file_ext;
                        //            parameters4[6].Value = modelt.down_num;
                        //            parameters4[7].Value = modelt.point;
                        //            parameters4[8].Value = modelt.user_name;
                        //            parameters4[9].Value = modelt.article_id;
                        //            DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                        //        }
                        //    }
                        //}
                        //#endregion

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.id;
        }

        /// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Models.report model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改主表数据==========================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update " + databaseprefix + "report set ");
                        strSql.Append("Instance_id=@Instance_id,");
                        strSql.Append("user_name=@user_name,");
                        strSql.Append("sex=@sex,");
                        strSql.Append("birthday=@birthday,");
                        strSql.Append("h_address=@h_address,");
                        strSql.Append("h_mobile=@h_mobile,");
                        strSql.Append("company=@company,");
                        strSql.Append("represent=@represent,");
                        strSql.Append("c_address=@c_address,");
                        strSql.Append("c_mobile=@c_mobile,");
                        strSql.Append("case_title=@case_title,");
                        strSql.Append("case_time=@case_time,");
                        strSql.Append("content=@content,");
                        strSql.Append("bm_yijian=@bm_yijian,");
                        strSql.Append("bm_remark=@bm_remark,");
                        strSql.Append("bm_qianm=@bm_qianm,");
                        strSql.Append("bm_time=@bm_time,");
                        strSql.Append("zf_remark=@zf_remark,");
                        strSql.Append("zf_qianm=@zf_qianm,");
                        strSql.Append("zf_time=@zf_time,");
                        strSql.Append("fg_yijian=@fg_yijian,");
                        strSql.Append("fg_remark=@fg_remark,");
                        strSql.Append("fg_qianm=@fg_qianm,");
                        strSql.Append("fg_time=@fg_time,");
                        strSql.Append("fgld_yijian=@fgld_yijian,");
                        strSql.Append("fgld_remark=@fgld_remark,");
                        strSql.Append("fgld_qianm=@fgld_qianm,");
                        strSql.Append("fgld_time=@fgld_time,");
                        strSql.Append("add_time=@add_time");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
                                    new SqlParameter("@Instance_id", SqlDbType.Int,4),
                                    new SqlParameter("@user_name", SqlDbType.NVarChar,100),
                                    new SqlParameter("@sex", SqlDbType.Int,4),
                                    new SqlParameter("@birthday", SqlDbType.NVarChar,20),
                                    new SqlParameter("@h_address", SqlDbType.NVarChar,255),
                                    new SqlParameter("@h_mobile", SqlDbType.NVarChar,20),
                                    new SqlParameter("@company", SqlDbType.NVarChar,100),
                                    new SqlParameter("@represent", SqlDbType.NVarChar,20),
                                    new SqlParameter("@c_address", SqlDbType.NVarChar,255),
                                    new SqlParameter("@c_mobile", SqlDbType.NVarChar,20),
                                    new SqlParameter("@case_title", SqlDbType.NVarChar,255),
                                    new SqlParameter("@case_time", SqlDbType.DateTime),
                                    new SqlParameter("@content", SqlDbType.NText),
                                    new SqlParameter("@bm_yijian", SqlDbType.Int,4),
                                    new SqlParameter("@bm_remark", SqlDbType.NText),
                                    new SqlParameter("@bm_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@bm_time", SqlDbType.DateTime),
                                    new SqlParameter("@zf_remark", SqlDbType.NText),
                                    new SqlParameter("@zf_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@zf_time", SqlDbType.DateTime),
                                    new SqlParameter("@fg_yijian", SqlDbType.Int,4),
                                    new SqlParameter("@fg_remark", SqlDbType.NText),
                                    new SqlParameter("@fg_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@fg_time", SqlDbType.DateTime),
                                    new SqlParameter("@fgld_yijian", SqlDbType.Int,4),
                                    new SqlParameter("@fgld_remark", SqlDbType.NText),
                                    new SqlParameter("@fgld_qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@fgld_time", SqlDbType.DateTime),
                                    new SqlParameter("@add_time", SqlDbType.DateTime),
                                    new SqlParameter("@id", SqlDbType.Int,4)};
                        parameters[0].Value = model.Instance_id;
                        parameters[1].Value = model.user_name;
                        parameters[2].Value = model.sex;
                        parameters[3].Value = model.birthday;
                        parameters[4].Value = model.h_address;
                        parameters[5].Value = model.h_mobile;
                        parameters[6].Value = model.company;
                        parameters[7].Value = model.represent;
                        parameters[8].Value = model.c_address;
                        parameters[9].Value = model.c_mobile;
                        parameters[10].Value = model.case_title;
                        parameters[11].Value = model.case_time;
                        parameters[12].Value = model.content;
                        parameters[13].Value = model.bm_yijian;
                        parameters[14].Value = model.bm_remark;
                        parameters[15].Value = model.bm_qianm;
                        parameters[16].Value = model.bm_time;
                        parameters[17].Value = model.zf_remark;
                        parameters[18].Value = model.zf_qianm;
                        parameters[19].Value = model.zf_time;
                        parameters[20].Value = model.fg_yijian;
                        parameters[21].Value = model.fg_remark;
                        parameters[22].Value = model.fg_qianm;
                        parameters[23].Value = model.fg_time;
                        parameters[24].Value = model.fgld_yijian;
                        parameters[25].Value = model.fgld_remark;
                        parameters[26].Value = model.fgld_qianm;
                        parameters[27].Value = model.fgld_time;
                        parameters[28].Value = model.add_time;
                        parameters[29].Value = model.id;
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        #endregion

                        #region 修改内容附件==========================
                        //删除已删除的附件
                        new case_attach().DeleteList_I(conn, trans, model.attach, model.Instance_id);
                        // 添加/修改附件
                        if (model.attach != null)
                        {
                            StringBuilder strSql4;
                            foreach (Models.case_attach modelt in model.attach)
                            {
                                strSql4 = new StringBuilder();
                                if (modelt.id > 0)
                                {
                                    strSql4.Append("update " + databaseprefix + "case_attach set ");
                                    strSql4.Append("instance_id=@instance_id,");
                                    strSql4.Append("step_id=@step_id,");
                                    strSql4.Append("file_name=@file_name,");
                                    strSql4.Append("file_path=@file_path,");
                                    strSql4.Append("file_size=@file_size,");
                                    strSql4.Append("file_ext=@file_ext,");
                                    strSql4.Append("point=@point,");
                                    strSql4.Append("user_name=@user_name,");
                                    strSql4.Append("article_id=@article_id");
                                    strSql4.Append(" where id=@id");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@step_id", SqlDbType.Int,4),
                                            new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                                            new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                                            new SqlParameter("@file_size", SqlDbType.Int,4),
                                            new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                                            new SqlParameter("@point", SqlDbType.Int,4),
                                            new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                                            new SqlParameter("@article_id", SqlDbType.Int,4),
                                            new SqlParameter("@id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.instance_id;
                                    parameters4[1].Value = modelt.step_id;
                                    parameters4[2].Value = modelt.file_name;
                                    parameters4[3].Value = modelt.file_path;
                                    parameters4[4].Value = modelt.file_size;
                                    parameters4[5].Value = modelt.file_ext;
                                    parameters4[6].Value = modelt.point;
                                    parameters4[7].Value = modelt.user_name;
                                    parameters4[8].Value = modelt.article_id;
                                    parameters4[9].Value = modelt.id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                                else
                                {
                                    strSql4.Append("insert into " + databaseprefix + "case_attach(");
                                    strSql4.Append("instance_id,step_id,file_name,file_path,file_size,file_ext,down_num,point,user_name,article_id)");
                                    strSql4.Append(" values (");
                                    strSql4.Append("@instance_id,@step_id,@file_name,@file_path,@file_size,@file_ext,@down_num,@point,@user_name,@article_id)");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@step_id", SqlDbType.Int,4),
                                            new SqlParameter("@file_name", SqlDbType.NVarChar,100),
                                            new SqlParameter("@file_path", SqlDbType.NVarChar,255),
                                            new SqlParameter("@file_size", SqlDbType.Int,4),
                                            new SqlParameter("@file_ext", SqlDbType.NVarChar,20),
                                            new SqlParameter("@down_num", SqlDbType.Int,4),
                                            new SqlParameter("@point", SqlDbType.Int,4),
                                            new SqlParameter("@user_name", SqlDbType.NVarChar,20),
                                            new SqlParameter("@article_id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.instance_id;
                                    parameters4[1].Value = modelt.step_id;
                                    parameters4[2].Value = modelt.file_name;
                                    parameters4[3].Value = modelt.file_path;
                                    parameters4[4].Value = modelt.file_size;
                                    parameters4[5].Value = modelt.file_ext;
                                    parameters4[6].Value = modelt.down_num;
                                    parameters4[7].Value = modelt.point;
                                    parameters4[8].Value = modelt.user_name;
                                    parameters4[9].Value = modelt.article_id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                            }
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //取得附件MODEL
            //List<Models.report_attach> attachList = new DAL.report_attach(databaseprefix).GetList(id);

            //删除附件
            StringBuilder strSql3 = new StringBuilder();
            strSql3.Append("delete from " + databaseprefix + "report_attach ");
            strSql3.Append(" where report_id=@report_id ");
            SqlParameter[] parameters3 = {
                    new SqlParameter("@report_id", SqlDbType.Int,4)};
            parameters3[0].Value = id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql3.ToString(), parameters3);
            sqllist.Add(cmd);

            //删除主表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "report ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                //new DAL.report_attach(databaseprefix).DeleteFile(attachList); //删除附件
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "report ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.report GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,Instance_id,user_name,sex,birthday,h_address,h_mobile,company,represent,c_address,c_mobile,case_title,case_time,content,bm_yijian,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fg_yijian,fg_remark,fg_qianm,fg_time,fgld_yijian,fgld_remark,fgld_qianm,fgld_time,add_time ");
            strSql.Append(" from " + databaseprefix + "report");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.report model = new Models.report();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.report GetModel_I(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,Instance_id,user_name,sex,birthday,h_address,h_mobile,company,represent,c_address,c_mobile,case_title,case_time,content,bm_yijian,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fg_yijian,fg_remark,fg_qianm,fg_time,fgld_yijian,fgld_remark,fgld_qianm,fgld_time,add_time  ");
            strSql.Append(" from " + databaseprefix + "report");
            strSql.Append(" where Instance_id=@Instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@Instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.report model = new Models.report();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.report GetModelCg(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,Instance_id,user_name,sex,birthday,h_address,h_mobile,company,represent,c_address,c_mobile,case_title,case_time,content,bm_yijian,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fg_yijian,fg_remark,fg_qianm,fg_time,fgld_yijian,fgld_remark,fgld_qianm,fgld_time,add_time ");
            strSql.Append(" from " + databaseprefix + "report");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)

            };
            parameters[0].Value = id;


            Models.report model = new Models.report();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.report GetModel(bool result, int wf_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,Instance_id,user_name,sex,birthday,h_address,h_mobile,company,represent,c_address,c_mobile,case_title,case_time,content,bm_yijian,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fg_yijian,fg_remark,fg_qianm,fg_time,fgld_yijian,fgld_remark,fgld_qianm,fgld_time,add_time ");
            strSql.Append(" from " + databaseprefix + "report");
            strSql.Append(" where Instance_id=@Instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@Instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = wf_id;

            Models.report model = new Models.report();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.report DataRowToModel(DataRow row)
        {
            Models.report model = new Models.report();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["Instance_id"] != null && row["Instance_id"].ToString() != "")
                {
                    model.Instance_id = int.Parse(row["Instance_id"].ToString());
                }
                if (row["user_name"] != null)
                {
                    model.user_name = row["user_name"].ToString();
                }
                if (row["sex"] != null && row["sex"].ToString() != "")
                {
                    model.sex = int.Parse(row["sex"].ToString());
                }
                if (row["birthday"] != null)
                {
                    model.birthday = row["birthday"].ToString();
                }
                if (row["h_address"] != null)
                {
                    model.h_address = row["h_address"].ToString();
                }
                if (row["h_mobile"] != null)
                {
                    model.h_mobile = row["h_mobile"].ToString();
                }
                if (row["company"] != null)
                {
                    model.company = row["company"].ToString();
                }
                if (row["represent"] != null)
                {
                    model.represent = row["represent"].ToString();
                }
                if (row["c_address"] != null)
                {
                    model.c_address = row["c_address"].ToString();
                }
                if (row["c_mobile"] != null)
                {
                    model.c_mobile = row["c_mobile"].ToString();
                }
                if (row["case_title"] != null)
                {
                    model.case_title = row["case_title"].ToString();
                }
                if (row["case_time"] != null && row["case_time"].ToString() != "")
                {
                    model.case_time = DateTime.Parse(row["case_time"].ToString());
                }
                if (row["content"] != null)
                {
                    model.content = row["content"].ToString();
                }
                if (row["bm_yijian"] != null && row["bm_yijian"].ToString() != "")
                {
                    model.bm_yijian = int.Parse(row["bm_yijian"].ToString());
                }
                if (row["bm_remark"] != null)
                {
                    model.bm_remark = row["bm_remark"].ToString();
                }
                if (row["bm_qianm"] != null)
                {
                    model.bm_qianm = row["bm_qianm"].ToString();
                }
                if (row["bm_time"] != null && row["bm_time"].ToString() != "")
                {
                    model.bm_time = DateTime.Parse(row["bm_time"].ToString());
                }
                if (row["zf_remark"] != null)
                {
                    model.zf_remark = row["zf_remark"].ToString();
                }
                if (row["zf_qianm"] != null)
                {
                    model.zf_qianm = row["zf_qianm"].ToString();
                }
                if (row["zf_time"] != null && row["zf_time"].ToString() != "")
                {
                    model.zf_time = DateTime.Parse(row["zf_time"].ToString());
                }
                if (row["fg_yijian"] != null && row["fg_yijian"].ToString() != "")
                {
                    model.fg_yijian = int.Parse(row["fg_yijian"].ToString());
                }
                if (row["fg_remark"] != null)
                {
                    model.fg_remark = row["fg_remark"].ToString();
                }
                if (row["fg_qianm"] != null)
                {
                    model.fg_qianm = row["fg_qianm"].ToString();
                }
                if (row["fg_time"] != null && row["fg_time"].ToString() != "")
                {
                    model.fg_time = DateTime.Parse(row["fg_time"].ToString());
                }
                if (row["fgld_yijian"] != null && row["fgld_yijian"].ToString() != "")
                {
                    model.fgld_yijian = int.Parse(row["fgld_yijian"].ToString());
                }
                if (row["fgld_remark"] != null)
                {
                    model.fgld_remark = row["fgld_remark"].ToString();
                }
                if (row["fgld_qianm"] != null)
                {
                    model.fgld_qianm = row["fgld_qianm"].ToString();
                }
                if (row["fgld_time"] != null && row["fgld_time"].ToString() != "")
                {
                    model.fgld_time = DateTime.Parse(row["fgld_time"].ToString());
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                //附件信息
                model.attach = new case_attach().GetList_I(model.Instance_id.ToString());
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,Instance_id,user_name,sex,birthday,h_address,h_mobile,company,represent,c_address,c_mobile,case_title,case_time,content,bm_yijian,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fg_yijian,fg_remark,fg_qianm,fg_time,fgld_yijian,fgld_remark,fgld_qianm,fgld_time,add_time ");
            strSql.Append(" FROM " + databaseprefix + "report ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,Instance_id,user_name,sex,birthday,h_address,h_mobile,company,represent,c_address,c_mobile,case_title,case_time,content,bm_yijian,bm_remark,bm_qianm,bm_time,zf_remark,zf_qianm,zf_time,fg_yijian,fg_remark,fg_qianm,fg_time,fgld_yijian,fgld_remark,fgld_qianm,fgld_time,add_time ");
            strSql.Append(" FROM " + databaseprefix + "report ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM " + databaseprefix + "report ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from " + databaseprefix + "report T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM " + databaseprefix + "report");
            strSql.Append(" where " + strWhere);
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion
    }
}
